home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2013 August / PC_Format_082013.iso / pene wersje / website x5 home 10 / wsx5_home.exe / {app} / Res / imemail.inc.php < prev    next >
PHP Script  |  2013-04-16  |  6KB  |  178 lines

  1. <?php
  2.       //Incomedia WebSite X5 EMail Class. All rights reserved.
  3.   
  4.     class imEMail {
  5.         var $from;
  6.         var $to;
  7.         var $subject;
  8.         var $charset;
  9.         var $text;
  10.         var $html;
  11.         var $type;
  12.         var $newline = "\r\n";
  13.         
  14.         var $attachments;
  15.         
  16.         function imEMail($from,$to,$subject,$charset) {
  17.             $this->from = $from;
  18.             $this->to = $to;
  19.             $this->charset = $charset;
  20.             $this->subject = strlen($subject) ? "=?" . strtoupper($this->charset) . "?B?". base64_encode($subject) . "?=" : "";
  21.         }
  22.  
  23.         /**
  24.          * Set the type of email standard (HTML, HTML-X or Text-only)
  25.          * @param [type] $type [description]
  26.          */
  27.         function setStandardType($type = "html") {
  28.             $this->type = $type;
  29.             $this->newline = (strtolower($type) == "html-x" ? "\n" : "\r\n");
  30.         }
  31.         
  32.         function setFrom($from) {
  33.             $this->from = $from;
  34.         }
  35.         
  36.         function setTo($to) {
  37.             $this->to = $to;
  38.         }
  39.         
  40.         function setSubject($subject) {
  41.             if (strlen($subject))
  42.                 $this->subject = "=?" . strtoupper($this->charset) . "?B?". base64_encode($subject) . "?=";
  43.             else
  44.                 $this->subject = "";
  45.         }
  46.         
  47.         function setCharset($charset) {
  48.             $this->charset = $charset;
  49.         }
  50.         
  51.         function setText($text) {
  52.             $this->text = $text;
  53.         }
  54.         
  55.         function setHTML($html) {
  56.             $this->html = $html;
  57.         }
  58.         
  59.         function attachFile($name,$content,$mime_type) {
  60.             if (strlen($name) === 0)
  61.                 return false;
  62.             $attachment['name'] = "=?" . strtoupper($this->charset) . "?B?". base64_encode($name) . "?=";
  63.             $attachment['content'] = base64_encode($content);
  64.             $attachment['mime_type'] = $mime_type;
  65.             $this->attachments[] = $attachment;
  66.         }
  67.         
  68.         function send() {
  69.             $headers = "";
  70.             $msg = "";
  71.  
  72.             if($this->from == "" || $this->to == "" || ($this->text == "" && $this->html == ""))
  73.                 return false;
  74.             
  75.             if ($this->type != "text") {
  76.  
  77.                 /*
  78.                 |-------------------------------
  79.                 | HTML/HTML-X email
  80.                 |-------------------------------
  81.                  */
  82.                 
  83.                 $boundary_file = md5(time() . "_attachment");
  84.                 $boundary_alt = md5(time() . "_alternative");            
  85.  
  86.                 $headers .= "From: " . $this->from . $this->newline;
  87.                 $headers .= "Message-ID: <" . time() . rand(0,9) . rand(0,9) . "@websitex5.users>" . $this->newline;
  88.                 $headers .= "X-Mailer: WebSiteX5 Mailer" . $this->newline;
  89.                 $headers .= "MIME-Version: 1.0" . $this->newline;
  90.  
  91.                 if(is_array($this->attachments)) {
  92.                     $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary_file . "\"" . $this->newline . $this->newline;
  93.                     $headers .= "--" . $boundary_file . $this->newline;
  94.                 }
  95.                 
  96.                 if($this->html == "") {
  97.                     $headers .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . $this->newline;
  98.                     if (strtolower($this->charset) != "utf-8")
  99.                         $headers .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  100.                     else
  101.                           $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  102.                     $msg .= $this->text . $this->newline . $this->newline;
  103.                 }
  104.                 else if($this->text == "") {
  105.                     $headers .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . $this->newline;
  106.                     if (strtolower($this->charset) != "utf-8")
  107.                         $headers .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  108.                     else
  109.                           $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  110.                     $msg .= $this->html . $this->newline . $this->newline;
  111.                 }
  112.                 else {
  113.                     $headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary_alt . "\"" . $this->newline;
  114.                     
  115.                     $msg .= "--" .$boundary_alt . $this->newline;
  116.                     $msg .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . $this->newline;
  117.                     if (strtolower($this->charset) != "utf-8")
  118.                         $msg .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  119.                     else
  120.                           $msg .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  121.                     $msg .= $this->newline;
  122.                     $msg .= $this->text . $this->newline . $this->newline;
  123.                     
  124.                     $msg .= "--" . $boundary_alt . $this->newline;
  125.                       $msg .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . $this->newline;
  126.                       if (strtolower($this->charset) != "utf-8")
  127.                         $msg .= "Content-Transfer-Encoding: 7bit" . $this->newline;
  128.                     else
  129.                           $msg .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  130.                     $msg .= $this->newline;
  131.                     $msg .= $this->html . $this->newline . $this->newline;
  132.                     
  133.                     $msg .= "--" . $boundary_alt . "--" . $this->newline . $this->newline;
  134.                 }
  135.                 
  136.                 if(is_array($this->attachments)) {
  137.                     foreach($this->attachments as $attachment) {
  138.                         $msg .= "--" . $boundary_file . $this->newline;
  139.                         $msg .= "Content-Type: " . $attachment["mime_type"] . "; name=\"" . $attachment["name"] . "\"" . $this->newline;
  140.                         $msg .= "Content-Transfer-Encoding: base64" . $this->newline;
  141.                         $msg .= "Content-Disposition: attachment; filename=\"" . $attachment["name"] . "\"" . $this->newline . $this->newline;
  142.                         $msg .= chunk_split($attachment["content"]) . $this->newline . $this->newline;
  143.                     }
  144.                     $msg .= "--" . $boundary_file . "--" . $this->newline . $this->newline;
  145.                 }
  146.                 
  147.                 if (function_exists('ini_set'))
  148.                     @ini_set("sendmail_from", $this->from);
  149.                 
  150.                 // First attempt: -f flag, no more headers
  151.                 if(@mail($this->to, $this->subject, $msg, $headers, "-f" . $this->from))
  152.                     return true;
  153.                 // Second attempt: no -f flag, no more headers
  154.                 if (@mail($this->to, $this->subject, $msg, $headers))
  155.                     return true;
  156.                 // Third attempt: no -f flag, one more To header
  157.                 $headers = "To: " . $this->to . $this->newline . $headers;
  158.                 return @mail($this->to, $this->subject, $msg, $headers);
  159.             } else {
  160.  
  161.                 /*
  162.                 |-------------------------------
  163.                 | Text-only email
  164.                 |-------------------------------
  165.                  */
  166.  
  167.                 $headers .= "From: " . $this->from . $this->newline;
  168.                 $headers .= "Content-Type: text/plain;charset=" . $this->charset . $this->newline;
  169.                 $msg .= $this->text . $this->newline . $this->newline;
  170.  
  171.                 $r = @mail($this->to, $this->subject, $msg, $headers);
  172.                 return $r;
  173.             }
  174.         }
  175.     }
  176.  
  177. // End of file imemail.inc.php
  178.